#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 即将拥有人鱼线的yjw
   @Time: 2024/12/6 10:14
   @File: client_id.py
   @desc:生成客户号、收益所有人编号
"""
from random import uniform, choice

import uuid

from DataTest.sqlmontage import randomInfo
from DataTest.tabledata.commonData.commonDict import convert_to_lowercase, get_id_type, get_occupation_code, \
    get_agency_no_code
from CommonScripts.blade import python_exec_sql
from DataTest.tabledata.commonData.randomInfo import get_random_number, get_bank, get_random_date


def get_client_id(env = None):
    '''
    生成客户号
    :param env: 环境信息
    :return: 交易类型编码
    '''
    sql = f"SELECT max(client_id)  as client_id FROM hsfund.fnd_client"
    try:
        result_info = python_exec_sql(sql, env=env)
        result = convert_to_lowercase(result_info)
        client_id = result[0]['client_id']
        if not client_id or (client_id == 'None') or (client_id == 'NUll') or (client_id == 'Null'):
            print('000000001000')
            return '000000001000'
        current_max_id = result[0]['client_id']
        # 将字符串形式的ID转换为整数并加1
        new_id_int = int(current_max_id) + 2
        # 确保新ID保持与原ID相同的位数，这里是12位
        new_id_str = str(new_id_int).zfill(len(current_max_id))
        return new_id_str

    except Exception as e:
        print(f"发生错误: {e}")

def get_beneficiary_id(env=None):
    '''
    生成受益所有人编号
    :param env: 环境信息
    :return: 受益所有人编号
    '''
    sql = f"select benefit_id  from hsfund.fnd_beneficiary order by  benefit_id desc"
    try:
        result_info = python_exec_sql(sql, env=env)
        result = convert_to_lowercase(result_info)
        if not result:
            return '#100'
        matching_items = [int(item['benefit_id'][1:]) if item['benefit_id'].startswith('#') else item for item in result]
        matching_items.sort(reverse=True)
        current_max_id = matching_items[0]
        return '#'+str(current_max_id+2)

    except Exception as e:
        print(f"发生错误: {e}")

def generate_fund_and_trade_accounts(client_type,env=None):
    fund_account = 'HS' + str(get_random_number(num_digits=2)) + str(get_random_number(num_digits=3)) + str(
        get_random_number(num_digits=2)) + str(get_random_number(num_digits=3))
    trade_account = str(get_random_number(num_digits=3)) + str(get_random_number(num_digits=5)) + str(
        get_random_number(num_digits=3)) + str(get_random_number(num_digits=5))
    bank_account = '622'+str(get_random_number(num_digits=3)) + str(get_random_number(num_digits=3)) + str(
        get_random_number(num_digits=3)) + str(get_random_number(num_digits=5))
    bankinfo = get_bank().split(' ')
    open_bank_name = bankinfo[0]  # 开户银行名称
    bank_code = bankinfo[1]  # 开户银行简称
    occur_date = get_random_date(20230102,20240717)
    dict_entries = {'fund_account': fund_account,
                    'trade_account': trade_account,
                    'bank_account':bank_account,
                    'open_bank_name':open_bank_name,
                    'bank_code':bank_code,
                    'occur_date':occur_date,
                    'ta_no':'HS',
                    'id_type': get_id_type(client_type, env=env),
                    'occupation_code': get_occupation_code(client_type, env=env),
                    'agency_no': get_agency_no_code(client_type, env=env)
                    }
    return dict_entries
def generate_account_request(client_type=None,env=None):
    occur_date = randomInfo.get_random_date(20230102, 20240717)
    dict_entries = {
                    'occur_date':occur_date,
                    'apply_date': occur_date,
                    'serial_no': uuid.uuid4().hex,
                    'apply_no': uuid.uuid4().hex
                    }
    return dict_entries

def get_apply_no():
    apply_no = 'ap'+ str(get_random_number(num_digits=3)) + str(get_random_number(num_digits=5)) + str(
        get_random_number(num_digits=3)) + str(get_random_number(num_digits=5))
    return apply_no


def generate_trade():
    occur_date = randomInfo.get_random_date(20230102,20240717)
    apply_share = randomInfo.get_random_number(6)
    net_value = round(uniform(1, 2), 4)
    match_amount = apply_share/net_value
    dict_entries = {
        'business_flag': choice(['01','02','03']),
        'apply_amount': match_amount,
        'match_amount': match_amount,
        'apply_share': apply_share,
        'match_volume': match_amount*net_value,
        'trade_status':'1',
        'serial_no':uuid.uuid4().hex,
        'apply_no':uuid.uuid4().hex,
        'confirm_no':uuid.uuid4().hex,
        'occur_date':occur_date,
        'apply_date':occur_date,
        'confirm_date':occur_date,
        'net_value':net_value,
        'bonus_mode':choice(['0','1','2','4'])

    }
    return dict_entries
    #if isinstance(keywords, (dict, list)):





if __name__ == '__main__':
    client_id = get_client_id('oracle')
    print(client_id)
    benefit_id = get_beneficiary_id('oracle')
    print(benefit_id)